package b1.scenes.home.police
{
	import b1.conifg.*;
	import b1.controls.tab.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.crime.*;
	import b1.scenes.detect.*;
	import b1.scenes.home.*;
	import b1.scenes.home.wanted.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.text.*;
	import flash.media.Sound;

	public class PoliceStation extends TabController
	{
		private static var instance:PoliceStation;
		private var parentScene:Sprite;
		
		private var casesInvestigated:Array;
		
		public function PoliceStation()
		{
			var TabControllerBackground:Class = ResourceHelper.getInstance().getClassByName("TabControllerBackground");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("TabTitleBackground");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton1");
			super(TabControllerBackground, CloseButton);
			var tab:Tab = new Tab("案件", 4, 2, 5, 3);
			this.addTab(TabTitleBackground, tab);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	titleText.width = 45;
        	titleText.wordWrap = true;
        	titleText.filters = [BFont.greenFontGlowFilter];
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 20, 0x000000, true, true);
			titleText.text = "我 被 立 案 追 查 的 案 件";
			titleText.x = 80;
			titleText.y = 60;
			addChild(titleText);
		}

		public static function getInstance():PoliceStation
		{
			if (!instance)
				instance = new PoliceStation();
			return instance;
		}
		
		public function open(parentScene:Sprite):void
		{
			if (!parentScene.contains(this))
			{
				this.parentScene = parentScene;
				var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
				var detectiveAnimalId:int = parseInt(player.ownAnimals[0]);
				ServerHelper.getInstance().getMyCases(onOpenComplete);
			}
		}

		private function onOpenComplete(battleCases:Object):void
		{
			for (var i:int; i < this.tabs.length; i++)
			{
				this.tabs[i].removeAll();
			}
			var casesClosed:Array = new Array();
			var casesInvestigated:Array = new Array();
			for (var caseId:String in battleCases)
			{
				var battleCase:BattleCaseInfo = battleCases[caseId];
				var caseItem:CaseItem = new CaseItem(battleCase);
				if (battleCase.clues.length > 0)
				{
					var clue:ClueInfo = battleCase.clues[battleCase.clues.length - 1];
					var leftMinutes:int = (clue.timeToDisappear - ServerHelper.getInstance().now()) / 60;
					if (leftMinutes <= 0 || battleCase.ownerDetectedFlag && battleCase.otherDetectedFlag)
					{
						//破案
						if (battleCase.detectiveOwnerId > 0)
						{
							this.tabs[0].addItem(caseItem, 125, 125);
						}
						//结案
						else
						{
							casesClosed.push(caseItem);
						}
					}
					//调查中
					else
					{
						casesInvestigated.push(caseItem);
					}
				}
			}
			//显示调查中的案件
			for(i = 0; i < casesInvestigated.length; i++)
			{
				caseItem = casesInvestigated[i] as CaseItem;
				this.tabs[0].addItem(caseItem, 125, 125);
			}
			//显示结案的案件
			for(i = 0; i < casesClosed.length; i++)
			{
				caseItem = casesClosed[i] as CaseItem;
				this.tabs[0].addItem(caseItem, 125, 125);
			}
			this.x = (BFont.stageWidth - this.width) / 2;
			this.y = 40;
			parentScene.addChild(this);

			//播放音效
			var SoundButton1:Class = ResourceHelper.getInstance().getClassByName("SoundButton1");
			var sound:Sound = new SoundButton1();
			sound.play(); 

			WantedTab.getInstance().close();
			Store.getInstance().close();
			CrimeDialog.getInstance().close();
			DetectDiaglog.getInstance().close();
			BottomNavigator.getInstance().hide();
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
	}
}